package com.example.button;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.location.GnssAntennaInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.widget.Toast;

import java.text.ChoiceFormat;

import static android.app.PendingIntent.getActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onFirstButtonClicked(View view){
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("旧版对话框")
                .setMessage("这是旧版的对话框，使用简单\n但是没有自动支持屏幕翻转！")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "你确定了！", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "你取消了！", Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    public void onSeconButtonClicked(View view){
        SimpleDialoggrament dialog = new SimpleDialoggrament();
        dialog.show(getSupportFragmentManager(), "simple");
    }

    public void onThildButtonClicked(View view){
        final String[] items = {"你觉得", "我觉得"};
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("你要你觉得还是我觉得？")
                .setItems(items, new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText (MainActivity.this, "我要"+ items[which], Toast.LENGTH_SHORT).show ();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "你取消了！", Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    int choice = 0;
    public void onFourthButtonClicked(View view){
        final String[] items = {"金斧头", "银斧头", "都不是我的"};
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("河翁:你丢的是哪把斧头？")
                .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        choice = which;
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                           if(choice == 2){
                               Toast.makeText(MainActivity.this, "你果然是个诚实的人，祝你早日找到斧头", Toast.LENGTH_SHORT).show();
                           }
                           else{
                               Toast.makeText(MainActivity.this, "你太不诚实了！这把" + items[choice] + "是不会给你的", Toast.LENGTH_SHORT).show();
                           }
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "你取消了！", Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    public void onFifthButtonClicked(View view){
        final String[] hobby = {"看电影", "逛街", "唱K", "玩游戏", "吃东西"};
        final boolean[] check = {false, false, false, false, false};
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle ("你空闲时喜欢做什么？")
                .setMultiChoiceItems (hobby, check, new DialogInterface.OnMultiChoiceClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        Log.e("Log", hobby[which]);
                    }
                })
                .setPositiveButton ("确定", new DialogInterface.OnClickListener () {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String answer = "你空闲时喜欢";
                int flag = 0;
                for (int n=0; n<check.length; n++){
                    if(check[n]){
                        if(flag == 0){
                            answer = answer + hobby[n];
                            flag++;
                        }
                        else{
                            answer = answer + ","+ hobby[n];
                            flag++;
                        }

                    }
                }
                if(flag == check.length){
                    Toast.makeText(MainActivity.this, "年轻人，我劝你耗子尾之，多学习！", Toast.LENGTH_SHORT).show();
                }
                else if(flag != 0 && flag != check.length){
                    Toast.makeText (MainActivity.this, answer, Toast.LENGTH_LONG).show ();
                }
                else{
                    Toast.makeText(MainActivity.this, "唉！还没选呢！", Toast.LENGTH_SHORT).show();
                }
            }
        })
        .show ();
    }

    public void onSixthButtonClicked(View view) {
        CustomDialog dialog = new CustomDialog();
        dialog.show(getSupportFragmentManager(), "unstop");
    }

}